/*
 * SQLiteDB.h
 *
 *  Created on: May 16, 2009
 *      Author: walter
 */

#ifndef SQLITEDB_H_
#define SQLITEDB_H_

#include <QString>
#include <QDir>

#include <iostream>
#include <map>
#include <string>
//#include <stdio.h>
#include <sqlite3.h>

#include "StaticModule.h"
#include "Options.h"

using namespace std;

enum QueryType
{
	//write only
	QueryT_Write,

	//read only
	QueryT_Read,

	QueryT_Last
};

#define DB StaticModule<SQLiteDB>::getInstance()

class SQLiteDB
{
public:
	SQLiteDB();
	virtual ~SQLiteDB();

	bool openDB(QString path, QString dbName);
	void closeDB(QString dbName);

	bool execQuery(QString command, QueryType queryType=QueryT_Last, QString dbName = QString()); //return true if query successfully
	bool quickUpdate(QString command){return execQuery(command, QueryT_Write, DB.mUsrMain);}
	bool quickGet(QString command){return execQuery(command, QueryT_Read, DB.mUsrMain);}

	int getRowCount(){return mRowCount;}
	int getColCount(){return mColumnCount;}
	QString getColName(int col);
	QString getTableValue(int row, int column);
	QString getTableValue(int row, QString columnName);

	QString getErrMsg(){return QString(mErrMsg);}

	QString mUsrMain;
	QString mUsrDict;

private:
	//DB<name(type), sqlite3 database>
	//The "common" database includes three tables: words, lists, exps. Every user need this database
	//The "<individual>" databases can have various names, because they are personal files for various users.
	map<QString, sqlite3*> mDBs;

	char* mErrMsg;
	char** mDBResult;
	bool mDBResultValid;
	int mRowCount;
	int mColumnCount;
	int mQueryResult;
	QueryType mLastQueryType;
	QString mLastQueryDB;
};

#endif /* SQLITEDB_H_ */
